package org.cresource.product.service.impl;

import org.cresource.basic.query.BaseQuery;
import org.cresource.basic.service.impl.BaseServiceImpl;
import org.cresource.basic.util.PageList;
import org.cresource.product.domain.Product;
import org.cresource.product.domain.ProductDetail;
import org.cresource.product.mapper.ProductDetailMapper;
import org.cresource.product.mapper.ProductMapper;
import org.cresource.product.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class ProductServiceImpl extends BaseServiceImpl<Product> implements IProductService {
    @Autowired
    private ProductDetailMapper detailMapper;
    @Autowired
    private ProductMapper productMapper;


    /**
     * 添加
     * 添加
     * 添加
     */
    @Override
    @Transactional
    public void add(Product product) {
        //先添加product
        productMapper.save(product);
        //在添加detail
        ProductDetail detail = product.getDetail();
        detail.setProductid(product.getId());
        detailMapper.save(detail);
    }

    /**
     * 修改
     * 修改
     * 修改
     */
    @Override
    public void update(Product product) {
        ProductDetail detail = product.getDetail();
        detail.setProductid(product.getId());
        if (detail.getId()!=null){
            System.out.println(product);
            detailMapper.update(product.getDetail());
        }else {
            detailMapper.save(detail);
        }
        //在添加detail
        productMapper.update(product);
    }

    @Override
    public Product getById(Long id) {
        return null;
    }

    /**
     * 上下架
     * 上下架
     * 上下架
     */
    @Override
    public void onOrOffSale(Map<String,Object> map) {
        //flag为1为上架 0为下架
        Map<String,Object> map1 = new HashMap<>();
        map.put("ids",map.get("ids"));
        Integer flag = (Integer)map.get("type");
        if (flag == 1){
            map.put("state",flag);
            map.put("onsaletime",new Date());
            productMapper.onsale(map);
        }else{
            map.put("state",flag);
            map.put("offsaletime",new Date());
            productMapper.offsale(map);
        }
    }



    /**
     * 删除
     * 删除
     * 删除
     */
    @Override
    public void delete(Long id) {
        productMapper.remove(id);
        //在添加detail
        detailMapper.remove(id);
    }

    @Override
    public List<Product> getAll() {
        return null;
    }

    @Override
    public PageList<Product> queryPage(BaseQuery query) {
        return null;
    }

    /**
     * 批量删除
     * 批量删除
     * 批量删除
     */
    @Override
    public void patchDelete(List<Long> ids) {
        productMapper.patchDelete(ids);
        //在添加detail
        detailMapper.patchDelete(ids);
    }




























    @Override
    public ProductDetail getDetailByProductId(Long productId) {
        return detailMapper.loadById(productId);
    }




}
